package com.teyao.teyao_back.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.teyao.teyao_back.config.myPage;
import com.teyao.teyao_back.entity.CarDeliverWrapper;
import com.teyao.teyao_back.entity.ResponseMessage;
import com.teyao.teyao_back.service.CarDeliverService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
@CrossOrigin
@RequestMapping("/deliver")
public class CarDeliverController {
    @Autowired
    private CarDeliverService carDeliverService;

    //    不定查询
    @GetMapping("/myQuery")
    public myPage MyQuery(Integer currentPage, Integer size, CarDeliverWrapper carDeliverWrapper) {
        return carDeliverService.myQuery(currentPage, size, carDeliverWrapper);
    }

    @GetMapping("/getNew")
    public List<CarDeliverWrapper> getUsersOrderedByAge() {
        QueryWrapper<CarDeliverWrapper> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("deliver_time");
        List<CarDeliverWrapper> list = carDeliverService.list(queryWrapper);
        System.out.println(list);
        return list;
    }

    //查所有
    @GetMapping("/list")
    public List<CarDeliverWrapper> selectAll() {
        return carDeliverService.list();
    }

    //    除了未配货
    @GetMapping("/selectAll")
    public myPage selectAll(Integer currentPage) {
        myPage myPage = new myPage();
        myPage.setCurrentPage(currentPage);
        Page<CarDeliverWrapper> page = new Page(myPage.getCurrentPage(), 5);
        QueryWrapper<CarDeliverWrapper> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne("status", "未配货");
        IPage result = carDeliverService.page(page, queryWrapper);
        myPage.setTotal((int) result.getTotal());
        myPage.setData(result.getRecords());
        return myPage;
    }

    @GetMapping("/selectByStatus")
    public myPage selectByStatus(Integer currentPage, String status) {
        myPage myPage = new myPage();
        myPage.setCurrentPage(currentPage);
        Page<CarDeliverWrapper> page = new Page(myPage.getCurrentPage(), 5);
        QueryWrapper<CarDeliverWrapper> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", status);
        IPage result = carDeliverService.page(page, queryWrapper);
        myPage.setTotal((int) result.getTotal());
        myPage.setData(result.getRecords());
        return myPage;

    }


    @GetMapping("/selectByDriver")
    public myPage selectBySDriver(Integer currentPage, String driver) {
        myPage myPage = new myPage();
        myPage.setCurrentPage(currentPage);

        Page<CarDeliverWrapper> page = new Page(myPage.getCurrentPage(), 5);
        QueryWrapper<CarDeliverWrapper> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("driver_name", driver);
        IPage result = carDeliverService.page(page, queryWrapper);
        myPage.setTotal((int) result.getTotal());
        myPage.setData(result.getRecords());
        return myPage;

    }
//    public ResponseMessage selectBySDriver(String driver) {
//        return carDeliverService.selectByDriver(driver);
//    }

    @GetMapping("/selectByNumber")
    public myPage selectByNumber(Integer currentPage, String number) {
        myPage myPage = new myPage();
        myPage.setCurrentPage(currentPage);

        Page<CarDeliverWrapper> page = new Page(myPage.getCurrentPage(), 5);
        QueryWrapper<CarDeliverWrapper> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("number", number);
        IPage result = carDeliverService.page(page, queryWrapper);
        myPage.setTotal((int) result.getTotal());
        myPage.setData(result.getRecords());
        return myPage;

    }

//    public ResponseMessage selectByNumber(String number) {
//        System.out.println(number);
//        return carDeliverService.selectByNumber(number);
//    }

    @GetMapping("/selectByOrderId")
    public ResponseMessage selectByOrderId(Integer orderId) {
        return carDeliverService.selectByOrderId(orderId);
    }

    @GetMapping("/selectDriving")
    public ResponseMessage selectDriving() {
        return carDeliverService.selectDriving();
    }

    @GetMapping("/selectNotStart")
    public ResponseMessage selectNotStart() {
        return carDeliverService.selectNotStart();
    }

    @GetMapping("/selectWillDriving")
    public ResponseMessage selectWillDriving() {
        ResponseMessage responseMessage = new ResponseMessage();
        QueryWrapper<CarDeliverWrapper> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", "待配送");
        responseMessage.setSuccess(true);
        responseMessage.setData(carDeliverService.list(queryWrapper));
        return responseMessage;
    }

}
